package com.shouhuyun.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shouhuyun.dao.PermissionDao;
import com.shouhuyun.entity.PageResult;
import com.shouhuyun.entity.QueryPageBean;
import com.shouhuyun.pojo.CheckItem;
import com.shouhuyun.pojo.Permission;
import com.shouhuyun.service.OrderSettingService;
import com.shouhuyun.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    // 增加权限
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    // 删除权限
    public Integer delete(Integer id) {

        // 1. 查看这个权限跟role角色是否有关
        Integer count = permissionDao.findCountByPermissionId(id);
        if (count > 0){
            // 说明该权限跟role有关
            return count; // 告知页面因为权限 跟 角色 关联导致失败
        }
        permissionDao.deleteById(id);
        return count;
    }

    // 查询所有权限
    public List<Permission> findAll() {
        List<Permission> list = permissionDao.findAll();
        return list;
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//查询条件
        //完成分页查询，基于mybatis框架提供的分页助手插件完成
        //提供当前页和页面大小
        PageHelper.startPage(currentPage,pageSize);
        //select * from t_checkitem limit 0,10

        if (queryString!= null){
            queryString = "%"+queryString+"%";
        }
        Page<Permission> page = permissionDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Permission> rows = page.getResult();
        return new PageResult(total,rows);
    }

    //根据ID查询
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    //编辑权限管理
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }


}
